using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using JHEMR.EmrSysUserCtl;
using JHEMR.EmrSysCom;

namespace JHEMR.EMREdit
{
    public partial class UCLabResult : UserControl
    {
        private string m_CurPatientID;
        private int m_CurVisitID;

        public UCLabResult()
        {
            InitializeComponent();
        }

        public void setPatient(string strPatientID, int intVisitID)
        {
            m_CurPatientID = strPatientID;
            m_CurVisitID = intVisitID;
            DataLoad();
        }

        private void DataLoad()
        {
            DataSet objDataSet = new DataSet();
            objDataSet = EmrSysWebservices.EmrSysWebservicesUse.myEmrGetLabDataSetByPatientID(m_CurPatientID, chkAll.Checked ? 0 : m_CurVisitID);
            
            if (objDataSet == null) return;
            
            if (objDataSet.Tables.Count > 0)
            {
                gcLabItem.DataSource = objDataSet.Tables[0];
                foreach (DataRow drCur in objDataSet.Tables[0].Rows)
                {
                    drCur["REQ_DEPT"] = EmrSysPubFunction.getDeptName(drCur["REQ_DEPT"].ToString(), false);
                    drCur["PERFORMED_BY"] = EmrSysPubFunction.getDeptName(drCur["performed_by"].ToString(), false);
                    drCur["REQ_PHYSICIAN"] = EmrSysPubFunction.getUserName(drCur["REQ_PHYSICIAN"].ToString(), true);
                }
            }
        }

        private void UCLabResult_Load(object sender, EventArgs e)
        {
            dgvItemResult.AutoGenerateColumns = false;
        }

        private void dgvLabResult_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvLabResult.SelectedRows.Count > 0)
            {
                DataGridViewRow objCurRow = dgvLabResult.SelectedRows[0];
                DataSet objDataSet = new DataSet();
                string strTestNo = objCurRow.Cells["REPORT_ITEM_CODE"].Value.ToString().Trim();
                lblItemName.Text = "【" + objCurRow.Cells["REPORT_ITEM_NAME"].Value.ToString() + "】历史数据：";
                
                objDataSet = EmrSysWebservices.EmrSysWebservicesUse.myEmrGetLabItemResultQuery(m_CurPatientID, m_CurVisitID, strTestNo);
                
                if (objDataSet != null && objDataSet.Tables.Count > 0)
                {
                    DataTable objTable = objDataSet.Tables[0];
                    dgvItemResult.DataSource = objTable.DefaultView;
                    
                    string strTitle = objCurRow.Cells["REPORT_ITEM_NAME"].Value.ToString() + "：正常值【" + objCurRow.Cells["result_range"].Value.ToString() + "】";
                    ucChart1.SetChartDataSet(objDataSet, strTitle);
                }
            }
        }

        private void dgvItemResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // 单元格格式化逻辑
        }

        private void cbxImage_CheckedChanged(object sender, EventArgs e)
        {
            ucChart1.Visible = cbxImage.Checked;
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            DataLoad();
        }

        private void gvLabItem_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gvLabItem.SelectedRowsCount < 1)
                return;
                
            DataRow drSelect = gvLabItem.GetDataRow(gvLabItem.FocusedRowHandle);
            DataSet objDataSet = new DataSet();
            objDataSet = EmrSysWebservices.EmrSysWebservicesUse.myEmrGetLabReportDataSet(drSelect["ORDER_SN"].ToString());
            
            foreach (DataRow dr in objDataSet.Tables[0].Rows)
            {
                if (dr["STATUS"] != DBNull.Value)
                {
                    if (dr["STATUS"].ToString() == "H")
                        dr["STATUS"] = "↑";
                    else if (dr["STATUS"].ToString() == "L")
                        dr["STATUS"] = "↓";
                    else
                        dr["STATUS"] = "";
                }
            }
            
            if (objDataSet.Tables.Count > 0)
            {
                dgvLabResult.DataSource = objDataSet.Tables[0];
                if (objDataSet.Tables[0].Rows.Count > 0)
                {
                    xtraTabControl1.SelectedTabPageIndex = 0;
                }
            }
            
            // 取结果
            object[] objList = new object[2];
            objList[0] = "LAB_REPORT_RESULT";
            objList[1] = drSelect["ORDER_SN"].ToString();
            DataSet dsReport = new DataSet();
            dsReport = EmrSysWebservices.EmrSysWebservicesUse.myEmrGenralDataSet(objList);
            
            if (dsReport.Tables.Count > 0)
            {
                dgvReportResult.DataSource = dsReport.Tables[0];
                if (dsReport.Tables[0].Rows.Count > 0)
                {
                    xtraTabControl1.SelectedTabPageIndex = 1;
                }
            }
        }

        private void dgvLabResult_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            int columnIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;
            if (rowIndex < 0) return;
            
            DataGridViewRow theRow = dgvLabResult.Rows[rowIndex];
            string strAbnormalIndicator = theRow.Cells["STATUS"].Value.ToString();
            
            if (strAbnormalIndicator == "↑")
                theRow.DefaultCellStyle.BackColor = Color.Red;
            if (strAbnormalIndicator == "↓")
                theRow.DefaultCellStyle.BackColor = Color.Green;
        }

        private void ucChart1_Load(object sender, EventArgs e) { }

        // 假设的控件声明
        private CheckBox chkAll;
        private DataGridView dgvLabResult;
        private DataGridView dgvItemResult;
        private DataGridView dgvReportResult;
        private Label lblItemName;
        private UCChart ucChart1;
        private DevExpress.XtraGrid.GridControl gcLabItem;
        private DevExpress.XtraGrid.Views.Grid.GridView gvLabItem;
        private DevExpress.XtraTab.XtraTabControl xtraTabControl1;
        private CheckBox cbxImage;
    }
}